#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/openresty-vhosts/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x

fn-openresty-vhosts-copy-from-image() {
  declare APP="$1" IMAGE_NAME="$2"
  local CONF_PATH="openresty/http-includes"
  local LOCATION_CONF_PATH="openresty/http-location-includes"

  mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP"
  find "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/" -maxdepth 1 -name 'openresty-http-includes.*' -type d -exec rm -r {} +
  copy_dir_from_image "$IMAGE_NAME" "$CONF_PATH" "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID" 2>/dev/null || true
  if [[ ! -f "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID" ]]; then
    touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID.missing"
  fi

  find "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/" -maxdepth 1 -name 'openresty-location-includes.*' -type d -exec rm -r {} +
  copy_dir_from_image "$IMAGE_NAME" "$LOCATION_CONF_PATH" "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID" 2>/dev/null || true
  if [[ ! -f "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID" ]]; then
    touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID.missing"
  fi
}

fn-openresty-vhosts-copy-from-directory() {
  declare APP="$1" SOURCECODE_WORK_DIR="$2"
  local CONF_PATH="openresty/http-includes"
  local LOCATION_CONF_PATH="openresty/http-location-includes"

  if [[ -d "$CONF_PATH" ]]; then
    pushd "$SOURCECODE_WORK_DIR" >/dev/null
    find "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/" -maxdepth 1 -name 'openresty-http-includes.*' -type d -exec rm -r {} +
    mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID/"
    cp -f "$CONF_PATH"/* "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID/"
    popd &>/dev/null || pushd "/tmp" >/dev/null
  else
    touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-http-includes.$DOKKU_PID.missing"
  fi

  if [[ -d "$LOCATION_CONF_PATH" ]]; then
    pushd "$SOURCECODE_WORK_DIR" >/dev/null
    find "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/" -maxdepth 1 -name 'openresty-location-includes.*' -type d -exec rm -r {} +
    mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID/"
    cp -f "$LOCATION_CONF_PATH"/* "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID/"
    popd &>/dev/null || pushd "/tmp" >/dev/null
  else
    touch "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP/openresty-location-includes.$DOKKU_PID.missing"
  fi
}

trigger-openresty-vhosts-core-post-extract() {
  declare desc="openresty-vhosts post-extract plugin trigger"
  declare trigger="post-extract"
  declare APP="$1" SOURCECODE_WORK_DIR="$2"
  local app_source_image

  mkdir -p "${DOKKU_LIB_ROOT}/data/openresty-vhosts/app-$APP"

  app_source_image="$(plugn trigger git-get-property "$APP" "source-image")"
  if [[ -n "$app_source_image" ]]; then
    fn-openresty-vhosts-copy-from-image "$APP" "$app_source_image"
  else
    fn-openresty-vhosts-copy-from-directory "$APP" "$SOURCECODE_WORK_DIR"
  fi
}

trigger-openresty-vhosts-core-post-extract "$@"
